<template>
  <div class="chart-container">
    <div ref="chart" class="chart"></div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';

const chart = ref(null);

//使用ref来动态接收数据
const dropoutNumber = ref(1982);
const joinedNumber = ref(25174);
const totalNumber = ref(dropoutNumber.value + joinedNumber.value);

//后端获取数据的函数
const fetchData = async () => {
  dropoutNumber.value = 1982;
  joinedNumber.value = 25174;
  totalNumber.value = dropoutNumber.value + joinedNumber.value;
};

onMounted(() => {
  fetchData();  
  const myChart = echarts.init(chart.value);
  myChart.setOption({
    title: {
      text: '退班率',
      left: 'center',
      textStyle: {
        color: '#333',
        fontWeight: 'bold'
      }
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      textStyle: {
        color: '#666'
      }
    },
    series: [
      {
        name: '班级统计',
        type: 'pie',
        radius: '55%',
        center: ['50%', '60%'],
        data: [
          { value: dropoutNumber.value, name: '退班人数' },
          { value: joinedNumber.value, name: '加入班级人数' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 20,
            shadowOffsetX: 0,
            // shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  });
});
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 30vh;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  /* background-color: #f0f0f0;  */
}

.chart {
  width: 100%;
  height: 100%;
}
</style>
